import 'package:http/http.dart' as http;
//第三方庫http网络请求简单封装
class HttpRequest {
  static String baseUrl() {
    return 'http://lmk.test-95155.com/';
  }

  //post请求
  static void post(String url, Function callBack,
      {Map<String, String> params, Function errorCallback}) async {
    try {
      http.Response response = await http.post(baseUrl() + url, body: params);
      if (callBack != null) {
        callBack(response.body);
      }
    } catch (e) {
      if (errorCallback != null) {
        callBack(e);
      }
    }
  }

//  get请求
  static void get(String url, Function callBack,
      {Map<String, String> params, Function errorCallback}) async {
    //处理get请求参数拼接url之后
    if (params != null && params.isNotEmpty) {
      //使用字符串缓冲
      StringBuffer urlParams = new StringBuffer("?");
      params.forEach((key, value) {
        urlParams.write(key + "=" + value + "&");
      });
      //刪除最後一個&
      String a = urlParams.toString();
      a = a.substring(0, a.length - 1);
      url += a;
      print(123);
      print(a);
    }

    try {
      http.Response response = await http.get(baseUrl() + url);
      if (callBack != null) {
        callBack(response.body);
      }
    } catch (e) {
      if (errorCallback != null) {
        callBack(e);
      }
    }
  }
}
